package coupon

import (
	"context"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/enum/errorcode"
	"mall/service/backend/common/errorx"
	"mall/service/promotion/rpc/promotionclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetachProductFromCouponLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDetachProductFromCouponLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetachProductFromCouponLogic {
	return &DetachProductFromCouponLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DetachProductFromCouponLogic) DetachProductFromCoupon(req *types.DetachIDsReq) (*types.BaseMsgResp, error) {
	if req.Id == nil || *req.Id == 0 || (len(req.BrandIds) == 0 && len(req.CategoryIds) == 0 && len(req.Ids) == 0) {
		return nil, errorx.NewCodeError(errorcode.InvalidArgument,
			l.svcCtx.Trans.Trans(l.ctx, "common.validationError"))
	}

	res, err := l.svcCtx.PromotionRpc.DetachProductFromCoupon(l.ctx, &promotionclient.DetachIDsReq{
		Id:          req.Id,
		BrandIds:    req.BrandIds,
		CategoryIds: req.CategoryIds,
		Ids:         req.Ids,
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Success: true,
		Msg:     l.svcCtx.Trans.Trans(l.ctx, res.Msg),
	}, nil
}
